package gui.other;

import j_util.Utiles;
import model.Premisa;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * Dialogo que permite abrir una premisa;
 *
 * @author Jose Martin Rozanec;
 */
public class OpenPremisaDialog extends JDialog {
    private javax.swing.JButton acceptButton;
    private javax.swing.JButton cancelButton;

    private javax.swing.JPanel operadoresPanel;
    private javax.swing.JPanel filechooserPanel;

    private JFileChooser fileChooser;
    private boolean accepted;
    private String filePath;

    /**
     * Costructor;
     *
     * @param fileChooser;
     */
    public OpenPremisaDialog(JFileChooser fileChooser) {
        this.fileChooser = fileChooser;
        initComponents();
        layoutComponents();
    }

    /**
     * Inicializacion de los componentes del OpenPremisaDialog;
     */
    private void initComponents() {
        setIconImage(Utiles.createImageIcon("/gui/images/other/logicaIcon.png").getImage());
        filechooserPanel = new javax.swing.JPanel();
        operadoresPanel = new javax.swing.JPanel();
        acceptButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        filePath = null;

        acceptButton.setText("Aceptar");
        acceptButton.setMnemonic(KeyEvent.VK_ENTER);
        acceptButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
//                if (operand == null) {
//                    JOptionPane.showMessageDialog(TruthTableOperatorsDialog.this, "Por favor, seleccione un operador que una las tablas!");
//                } else {
//                    if (filePath == null) {
//                        JOptionPane.showMessageDialog(TruthTableOperatorsDialog.this, "Por favor, abra un archivo que corresponda a una tabla de verdad!");
//                    } else {
//                        try {
//                            RandomAccessFile randomAccessFile = new RandomAccessFile(filePath, "rw");
//                            if (!randomAccessFile.readUTF().startsWith(TruthTable.openTag())) {
//                                JOptionPane.showMessageDialog(TruthTableOperatorsDialog.this, "Por favor, abra un archivo que corresponda a una tabla de verdad!");
//                            } else {
//                                accepted = true;
//                                beforeOperator = beforeCheckBox.isSelected();
//                                dispose();
//                            }
//                        } catch (FileNotFoundException e1) {
//                            JOptionPane.showMessageDialog(TruthTableOperatorsDialog.this, "Se ha producido un error al leer el archivo!", "", JOptionPane.WARNING_MESSAGE);
//                        } catch (IOException e1) {
//                            JOptionPane.showMessageDialog(TruthTableOperatorsDialog.this, "Se ha producido un error al leer el archivo!", "", JOptionPane.WARNING_MESSAGE);
//                        }
//                    }
//                }
            }
        });

        cancelButton.setText("Cancelar");
        cancelButton.setMnemonic(KeyEvent.VK_ESCAPE);
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                accepted = false;
                dispose();
            }
        });

        Dimension buttonsDimension = new Dimension(155, 25);

        acceptButton.setPreferredSize(buttonsDimension);
        cancelButton.setPreferredSize(buttonsDimension);
    }

    /**
     * Dispone los elementos visuales del dialogo;
     */
    private void layoutComponents() {
        filechooserPanel.setPreferredSize(new Dimension(240, 450));
        fileChooser.setControlButtonsAreShown(false);

        add(fileChooser, BorderLayout.CENTER);
        JList list = new JList(new String[]{"Hola", "Hola2"});
        list.setPreferredSize(new Dimension(100, 450));
        add(list, BorderLayout.EAST);
        operadoresPanel.setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        int gridx = 0;
        int gridy = 0;
        gridBagConstraints.gridy = gridy;
        gridBagConstraints.gridx = gridx;
        //todo START FILECHOOSER PANEL
        gridBagConstraints.gridwidth = 4;
        operadoresPanel.add(new JLabel(Utiles.createImageIcon("/gui/images/truth_table/separator.png")), gridBagConstraints);
        gridx = 0;
        gridBagConstraints.gridy = ++gridy;
        gridBagConstraints.gridwidth = 1;
//            //todo END OF FILECHOOSER PANEL
//            gridBagConstraints.gridheight = 1;
//            gridBagConstraints.gridwidth = 1;
//            operadoresPanel.add(negacionButton, gridBagConstraints);
//            gridBagConstraints.gridx = ++gridx;
//            operadoresPanel.add(disyuncionIncButton, gridBagConstraints);
//            gridBagConstraints.gridx = ++gridx;
//            operadoresPanel.add(condicionalButton, gridBagConstraints);
//            gridBagConstraints.gridx = ++gridx;
//            operadoresPanel.add(negacionConjuntaButton, gridBagConstraints);
//            gridx = 0;
//            gridBagConstraints.gridy = ++gridy;
//            gridBagConstraints.gridx = gridx;
//            operadoresPanel.add(conjuncionButton, gridBagConstraints);
//            gridBagConstraints.gridx = ++gridx;
//            operadoresPanel.add(disyuncionExcButton, gridBagConstraints);
//            gridBagConstraints.gridx = ++gridx;
//            operadoresPanel.add(bicondicionalButton, gridBagConstraints);
//            gridBagConstraints.gridx = ++gridx;
//            operadoresPanel.add(incompatibilidadButton, gridBagConstraints);
//            gridx = 0;
//            gridBagConstraints.gridy = ++gridy;
//            gridBagConstraints.gridx = gridx;
//            gridBagConstraints.gridwidth = 4;
//            operadoresPanel.add(new JLabel(Utiles.createImageIcon("/gui/images/truth_table/separator.png")), gridBagConstraints);
//            gridx = 0;
//            gridBagConstraints.gridy = ++gridy;
//            gridBagConstraints.gridx = gridx;
//            gridBagConstraints.gridwidth = 3;
//            operadoresPanel.add(beforeCheckBox, gridBagConstraints);
//            gridx = 0;
//            gridBagConstraints.gridy = ++gridy;
//            gridBagConstraints.gridx = gridx;
//        gridBagConstraints.gridwidth = 4;
//        operadoresPanel.add(new JLabel(Utiles.createImageIcon("/gui/images/truth_table/separator.png")), gridBagConstraints);
//        gridBagConstraints.gridy = ++gridy;
//        gridBagConstraints.gridx = ++gridx;
//        gridBagConstraints.gridx = ++gridx;
//        gridBagConstraints.gridwidth = 1;
        operadoresPanel.add(acceptButton, gridBagConstraints);
        gridBagConstraints.gridx = ++gridx;
        operadoresPanel.add(cancelButton, gridBagConstraints);

        add(operadoresPanel, BorderLayout.PAGE_END);
    }

    /**
     * Permite conocer si se ha aceptado o no;
     *
     * @return boolean accepted;
     */
    public boolean isAccepted() {
        return accepted;
    }

    /**
     * Permite acceder a la Premisa contenida en el archivo abierto;
     *
     * @return Premisa;
     */
    public Premisa getPremisa() {
        try {
            RandomAccessFile randomAccessFile = new RandomAccessFile(filePath, "rw");
            return Premisa.parseXML(randomAccessFile.readUTF());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        OpenPremisaDialog truthTableOperatorsDialog = new OpenPremisaDialog(new JFileChooser());
        truthTableOperatorsDialog.setModal(true);
        truthTableOperatorsDialog.setPreferredSize(new Dimension(640, 490));
        truthTableOperatorsDialog.setMinimumSize(new Dimension(640, 490));
        truthTableOperatorsDialog.setVisible(true);
        System.exit(0);
    }
}